﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.DAL.DocumentCenter;

namespace MS.InformationManagement.BLL.ConfirmationMessage
{
    public class ConfirmationMessage
    {
        public static bool SendToPrintItem(Guid IdList, int IdItem, bool IsCopy, int ItemIdDevoluciones )
        {
            bool result = false;

            SPSecurity.RunWithElevatedPrivileges(delegate()
             {
                 using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
                 {
                     using (SPWeb currentWeb = currentSite.OpenWeb())
                     {
                         SPList documentsList = SPContext.Current.Web.Lists[IdList];
                         SPListItem documentCopiaListItem = documentsList.GetItemById(IdItem);
                         if(!IsCopy)
                             documentCopiaListItem[CustomSiteColumns.FIELD_DOCUMENT_PRINTDATE] = null;
                         else
                             documentCopiaListItem[CustomSiteColumns.FLD_COPY_DOCUMENT_PRINTDATE] = null;

                         documentCopiaListItem.Update();

                         result = setDevoluciones(ItemIdDevoluciones);
                     }
                 }
             });

            return result;
        }

        private static bool setDevoluciones(int id)
        {
            bool result = false;
            using (var dc = new DCDocumentCenterDataContext(SPContext.Current.Web.Url))
            {
                var entity = (from item in dc.Devoluciones
                              where item.Id == id
                              select item).FirstOrDefault();

                entity.EnvíoActivo = false;
                dc.SubmitChanges();

                result = true;
            }

            return result;
        }
    }
}
